var oldAnchor = "";
$(document).ready(function() {

	$('nav ul').children().width(160);
	$('nav ul').kwicks({max: 320, min:120, duration: 400, easing: 'easeOutQuad', complete: 'callback'});

	function loadPage(anchor,animation){
		var page = anchor.split('-')[0];
		//Gestion des partie de aam
		if(oldAnchor != page){
			var toLoad = page+'.html';
			//Gestion du menu
			$("nav ul").children().removeClass("selected");
			$("nav ul li a[href="+page+".html]").parent().addClass("selected");
			$("nav ul li a[href="+page+".html]").parent().trigger("mouseover",animation);

			if(animation!=false){
				$('body').append('<article id="load">LOADING...</article>');
			}
			loadContent(toLoad,animation,anchor);
		}
		else if(page == "aam" && anchor != page){
			showAAMArticle(anchor,500);
		}
		else if(page == "aam" && anchor == page){
			closeAAMArticle(500);
		}
		oldAnchor = page;
	}
	function loadContent(toLoad,animation,anchor) {
		$.get(toLoad,function(data){
			var htdata = jQuery(data);
			var content = htdata.find('article').parent().html();
			var title = htdata.find('div#title').html();
			if($('section#main').html() != content){
				if(animation==false){
					$('section#main').html(content);
					if(anchor.slice(0,3) == "aam" && anchor.length > 3){
						showAAMArticle(anchor,0);
					}
					else if(anchor.slice(0,3) == "aam"){
						closeAAMArticle(0);
					}
				}
				else{
					$('section').slideUp(500).queue(function(){
						$(this).html(content);
						jQuery.dequeue(this);
						if(anchor.slice(0,3) == "aam" && anchor.length > 3){
							showAAMArticle(anchor,0);
						}
						else if(anchor.slice(0,3) == "aam"){
							closeAAMArticle(0);
						}
					});
				}
			}
			if($('div#title').html() != title){
				if(animation==false){
					$('div#title').html(title);
				}
				else{
					$('div#title').fadeOut(500).queue(function(){
						$(this).html(title);
						$(this).fadeIn(500);
						jQuery.dequeue(this);
					});
				}
			}
			showNewContent(animation);
		});
	}
	function showNewContent(animation) {
		if(animation==false){
			$('section').show();
		}
		else{
			$('section').slideDown(500).queue(function(){
				$('#load').remove();
				jQuery.dequeue(this);
			});
		}
	}

	$(document).hashchange( function(data){
		loadPage(location.hash.slice(1));
		return false;
	});
	if(location.hash.length>0){
		loadPage(location.hash.slice(1),false);
	}
	else{
		$("nav ul li.selected").trigger("mouseover",false);
	}

	$('#logo-small').click(function(){
		location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
		return false;
	});
	
	$('nav li a').click(function(){
		location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
		return false;
	});

	function closeAAMArticle(delay){
		if($('article.toogle')){
			$('article.toogle div').slideUp(delay);
			updateIdAAM();
		}
	}

	function showAAMArticle(selected,delay){
		closeAAMArticle(delay);
		var anchor = "#"+selected+"-article";
		$(anchor +' div').slideDown(delay).queue(function(){
			jQuery.dequeue(this);
		});
		scrollToAnchor(anchor,delay);
	}

	function updateIdAAM(){
		$('article.toogle').each(function(){
			if($(this).attr('id').slice(-7) != "article"){
				$(this).attr('id',$(this).attr('id')+"-article");
			}
		});
	}

	closeAAMArticle(0);
});
function scrollToAnchor(anchor,delay){
	var index = $(anchor).parent().children().index($(anchor));
	var position;
	if(index == 0){
		position = $(anchor).offset().top;
	}
	else{
		var tmp = $($(anchor).parent().children().get(index-1));
		position = tmp.offset().top + $(tmp.children()[0]).outerHeight();
	}
	$('html,body').animate({
			scrollTop: position
	}, delay);
}
